﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Microsoft.Reporting.WinForms;
using System.Data;
using Aulac.Global.Xml;

namespace Aulac.Client.Reports.Ems.WinFrmBase
{
    partial class RptViewerBase
    {
        #region Virtual
        protected void OnRefreshrptViewer(XmlDataDocument xmlData, string fileName)
        {
            OnShowReport(xmlData, fileName);
        }        
        #endregion
        #region Methods
        public void OnShowReport(XmlDataDocument xmlData, string fileName)
        {
            XmlDataDocument xmlDoc = xmlData;
            rptViewer.ProcessingMode = ProcessingMode.Local;
            rptViewer.LocalReport.ReportPath = fileName;
            DumpParameterInfo(rptViewer.ServerReport);
            rptViewer.LocalReport.DataSources.Add(new ReportDataSource("Departments", LoadDepartmentsData(xmlData)));
            rptViewer.Drillthrough += new DrillthroughEventHandler(ProcessDrillthroughReprot);
            rptViewer.RefreshReport();
        }
        private DataTable LoadEmployeesData()
        {
            //DataSet dataSet = new DataSet();
            //dataSet.ReadXml(@"c:\employees.xml");
            //return dataSet.Tables[0];
            return null;
        }

        private DataTable LoadDepartmentsData(XmlDataDocument xmlData)
        {
            try
            {
                DataSet dataSet = new DataSet();
                //dataSet.ReadXml(@"c:\departments.xml");
                //return dataSet.Tables[0];
                //DataTable dt = null;
                //XmlElement eLines = XmlProcess.GetVchLinesElm(xmlData);
                //XmlDataDocument xml = new XmlDataDocument();
                //xmlData.DataSet.Tables;
                //dataSet.ReadXml(eLines);
                //Aulac.Global.Data.DataTableProcess.AddDataInParentNodeToTable(eLines, ref dt);
                //return xmlData.DataSet.;
                return null;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            
        }

        void ProcessDrillthroughReprot(object sender, DrillthroughEventArgs e)
        {
            LocalReport localReport = (LocalReport)e.Report;
            localReport.DataSources.Add(new ReportDataSource("Employees", LoadEmployeesData()));
        }
        private void Test()
        {
            
        }

        public void DumpParameterInfo(ServerReport sReport)
        {
            string str = "";
            ReportParameterInfoCollection pInfo = sReport.GetParameters();

            if (pInfo.Count == 0)
            {
                str +="<No parameters are defined for this report>";
            }
            else
            {
                
                str +="===========================================================================";
                str +="Parameter Info for " + sReport.ReportPath;

                foreach (ReportParameterInfo p in pInfo)
                {
                    str +="----------------------------------------------------------------------";
                    str +=string.Format("Parameter Name: {0}", p.Name);
                    str +=string.Format("Data Type: {0}", p.DataType);
                    str +=string.Format("State: {0}", p.State);
                    str +=string.Format("Allow Blank? {0}", p.AllowBlank);
                    str +=string.Format("Nullable? {0}", p.Nullable);
                    str +=string.Format("Prompt User? {0}", p.PromptUser);
                    str +=string.Format("User Prompt: {0}", p.Prompt);
                    str +=string.Format("Visible? {0}", p.Visible);
                    str +=string.Format("MultiValued? {0}", p.MultiValue);
                    str +=string.Format("Default values query-based? {0}", p.AreDefaultValuesQueryBased);
                    str +=string.Format("Default value(s): ");

                    // Show a list of default values for the report params
                    IList<string> dvList = p.Values;
                    int t;

                    if (dvList.Count != 0)
                    {

                        t = 1;

                        foreach (string dv in dvList)
                        {
                            if (t != dvList.Count)
                            {
                                t++;
                                str +=dv + ", ";
                            }
                            else
                            {
                                if (t == 1)
                                    str +=dv;
                                else
                                    str +="or " + dv;
                            }
                        }
                    }
                    else
                    {
                        str +="<no default values defined>";
                    }

                    str += string.Format("Valid values query based? {0}", p.AreValidValuesQueryBased);
                    str +="Valid values: ";

                    // Show a list of valid values for the report params

                    IList<ValidValue> vvList = p.ValidValues;

                    if (vvList != null)
                    {

                        t = 1;

                        foreach (ValidValue vv in vvList)
                        {
                            if (t != vvList.Count)
                            {
                                t++;
                                str +=vv.Value + ", ";
                            }
                            else
                            {
                                if (t == 1)
                                    str +=vv.Value;
                                else
                                    str +="or " + vv.Value;
                            }
                        }
                    }
                    else
                    {
                        str +="<no valid values defined>";
                    }

                    str +="Dependent parameters: ";

                    ReportParameterInfoCollection dpInfo = p.Dependencies;
                    t = 1;

                    if (dpInfo.Count != 0)
                    {

                        foreach (ReportParameterInfo dp in dpInfo)
                        {
                            if (t != dpInfo.Count)
                            {
                                t++;
                                str +=dp.Name + ", ";
                            }
                            else
                            {
                                if (t == 1)
                                    str +=dp.Name;
                                else
                                    str +="or " + dp.Name;
                            }
                        }
                    }
                    else
                    {
                        str +="<no dependent parameters defined>";
                    }

                }

            }
            str +="----------------------------------------------------------------------";
        }
        #endregion
    }
}
